package com.zhangfb95.crosschain.server.infra.config.configuration;

import org.springframework.boot.web.server.WebServer;
import org.springframework.boot.web.server.WebServerException;

import java.util.HashMap;
import java.util.Map;

/**
 * 多个web服务器
 * @author zhangfubing
 * @since 2023/10/17
 */
public class MultiWebServer implements WebServer {

    private final Map<String, WebServer> webServerMap = new HashMap<>();

    public void putWebServer(String name, WebServer webServer) {
        this.webServerMap.put(name, webServer);
    }

    @Override
    public void start() throws WebServerException {
        for (Map.Entry<String, WebServer> webServer : webServerMap.entrySet()) {
            webServer.getValue().start();
        }
    }

    @Override
    public void stop() throws WebServerException {
        for (Map.Entry<String, WebServer> webServer : webServerMap.entrySet()) {
            webServer.getValue().stop();
        }
    }

    @Override
    public int getPort() {
        for (Map.Entry<String, WebServer> webServer : webServerMap.entrySet()) {
            return webServer.getValue().getPort();
        }
        return 0;
    }
}
